﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Hidistro.Core;
    using Hidistro.Entities;
    using Hidistro.Entities.Orders;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Member;
    using Hidistro.UI.Common.Controls;
    using System.Globalization;
    using System.Web.UI.HtmlControls;
    using Hidistro.UI.SaleSystem.Tags;
    using System.Collections.Generic;
    

    [ParseChildren(true)]
    public class OrderReturn : PaymentTemplatedWebControl
    {
        private Literal litMessage;
        private string orderId;
        private Common_UserGotoReview orderItems;

        public OrderReturn() : base(false)
        {
        }

        protected override void AttachChildControls()
        {
            this.litMessage = (Literal) this.FindControl("litMessage");
            this.orderId = this.Page.Request.QueryString["out_trade_no"];
            this.orderItems = (Common_UserGotoReview)this.FindControl("Common_UserGotoReview");

            JSShop.Model.Hishop_Orders oredermodel = new Juse.Interface.UserOrder().GetModelByOrderId(this.orderId);
            List<JSShop.Model.Hishop_OrderItems> list = new Juse.Interface.UserOrder().GetItemsByOrderId(this.orderId);

            if (!this.Page.IsPostBack)
            {
                //OrderInfo orderInfo = TradeHelper.GetOrderInfo(this.orderId);
                //if ((orderInfo == null) || (orderInfo.UserId != HiContext.Current.User.UserId))
                //{
                //    this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该订单不存在或者不属于当前用户的订单"));
                //}
                //else
                //{
                //    this.BindOrderItems(orderInfo);
                //}
                if ((oredermodel == null) || (oredermodel.UserId != HiContext.Current.User.UserId))
                {
                    this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该订单不存在或者不属于当前用户的订单"));
                }
                else
                {
                    this.orderItems.DataSource = list;
                    this.orderItems.DataBind();
                }
            }
        }

        //private void BindOrderItems(OrderInfo order)
        //{
        //    this.orderItems.DataSource = order.LineItems.Values;
        //    this.orderItems.DataBind();
        //}

        protected override void DisplayMessage(string status)
        {
            status = "success";
            switch (status)
            {
                case "ordernotfound":
                    this.litMessage.Text = string.Format("没有找到对应的订单信息，订单号：{0}", base.OrderId);
                    break;

                case "gatewaynotfound":
                    this.litMessage.Text = "没有找到与此订单对应的支付方式，系统无法自动完成操作，请联系管理员";
                    break;

                case "verifyfaild":
                    this.litMessage.Text = "支付返回验证失败，操作已停止";
                    break;

                case "success":
                    this.litMessage.Text = string.Format("恭喜您，订单已成功完成支付：{0}</br>支付金额：<h4>{1}</h4>", base.OrderId, this.Amount.ToString("F"));
                 
                    break;

                case "exceedordermax":
                    this.litMessage.Text = "订单为团购订单，订购数量超过订购总数，支付失败";
                    break;

                case "groupbuyalreadyfinished":
                    this.litMessage.Text = "订单为团购订单，团购活动已结束，支付失败";
                    break;

                case "fail":
                    this.litMessage.Text = string.Format("订单支付已成功，但是系统在处理过程中遇到问题，请联系管理员</br>支付金额：{0}", this.Amount.ToString("F"));
                    break;

                default:
                    this.litMessage.Text = "未知错误，操作已停止";
                    break;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-PaymentReturn.html";
            }
            base.OnInit(e);
        }
    }
}

